package com.jse.util.qrcode;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.nutz.dao.entity.Record;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.qrcode.QRCode;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;

@IocBean
public class Qrcode {

	Log log = Logs.get();

	/**
	 * 生成图像
	 * 
	 * @filePath 文件路径
	 * @content 二维码内容
	 * @throws WriterException
	 * @throws IOException
	 */
	public void NEW(String filePath, String content) {
		// String filePath = "D://zxing.png";
		// Record json = new Record();
		// json.put("url","https://github.com/zxing/zxing/tree/zxing-3.0.0/javase/src/main/java/com/google/zxing");
		// json.put("author", "shihy");
		// String content = Json.toJson(json);// 内容
		int width = 200; // 图像宽度
		int height = 200; // 图像高度
		String format = "png";// 图像类型
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

		try {
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
			MatrixToImageWriter.writeToFile(bitMatrix, format, new File(filePath));// 输出图像
			log.infof("二维码输出成功!路径:%", filePath);
		} catch (WriterException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 生成矩阵

	}

	/**
	 * 解析图像
	 * 
	 * @filePath 二维码文件路径
	 */
	public Record decode(String filePath) {
		// String filePath = "D://zxing.png";
		BufferedImage image;
		try {
			image = ImageIO.read(new File(filePath));
			LuminanceSource source = new BufferedImageLuminanceSource(image);
			Binarizer binarizer = new HybridBinarizer(source);
			BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
			Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
			hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
			Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
			Record content = Json.fromJson(Record.class, result.getText());
			log.info("图片中内容：  ");
			log.info("author： " + content.getString("author"));
			log.info("zxing：  " + content.getString("zxing"));
			log.info("图片中格式：  ");
			log.info("encode： " + result.getBarcodeFormat());
			return content;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void sc() {
		// 直接获得 QRCode 的图像对象
		// BufferedImage image = QRCode.toQRCode("this's simple text.");

		// 或者通过构造器模式
		QRCode qrcode = QRCode.NEW("this's simple text by used build method.");

		// 获得 QRCode 的图像对象
		// BufferedImage image = qrcode.getQrcodeImage();

		// 在指定路径的文件中生成一个 QRCode 的图片，图片格式为 png
		// 如果指定的文件没有扩展名，将生成一个扩展名为 png 的图片
		String filePath = "/PATH/TO/FILE.jpg";
		qrcode.toFile(filePath);

		// 或者直接指定一个 File 对象
		File file = new File(filePath);
		qrcode.toFile(file);
	}

	public static void main(String[] args) {
		// QRCode qrcode = QRCode.NEW("this's simple text by used build method.");
		// String filePath = "d:/FILE.jpg";
		// qrcode.toFile(filePath);
		Qrcode q = new Qrcode();
		q.decode("d:/zxing.png");
	}
}
